

package com.zdkj.subway.support.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.common.enums.LogEnum;
import com.zdkj.subway.support.entity.SwPmTvmAccountLog;
import com.zdkj.subway.support.service.SwPmTvmAccountLogService;
import com.zdkj.subway.support.util.DateUtilSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * TVM平账日志表
 *
 * @author cuilei
 * @date 2020-06-17 15:32:02
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swpmtvmaccountlog")
@Api(value = "swpmtvmaccountlog", tags = "TVM平账日志表管理")
public class SwPmTvmAccountLogController {

    private final SwPmTvmAccountLogService swPmTvmAccountLogService;

    /**
     * 平账日志列表分页查询
     *
     * @param page              分页对象
     * @param swPmTvmAccountLog TVM平账日志表
     * @return
     */
    @ApiOperation(value = "平账日志列表分页查询", notes = "平账日志列表分页查询")
    @GetMapping("/page")
    public R getSwPmTvmAccountLogPage(Page page, SwPmTvmAccountLog swPmTvmAccountLog) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.eq("log_status",LogEnum.UN_RECORD.getCode());
        this.getQuery(swPmTvmAccountLog,queryWrapper);
        return R.ok(swPmTvmAccountLogService.page(page, queryWrapper));
    }

    /**
     * 平账日志归档记录分页查询
     *
     * @param page
     * @param swPmTvmAccountLog
     * @return
     */
    @ApiOperation(value = "平账日志归档记录分页查询", notes = "平账日志归档记录分页查询")
    @GetMapping("/getRecord")
    public R getSwPmTvmAccountLogRecordPage(Page page, SwPmTvmAccountLog swPmTvmAccountLog) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.eq("log_status",LogEnum.RECORD.getCode());
        this.getQuery(swPmTvmAccountLog,queryWrapper);
        return R.ok(swPmTvmAccountLogService.page(page, queryWrapper));
    }

    /**
     * 通过id查询TVM平账日志表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmTvmAccountLogService.getById(id));
    }

    /**
     * 新增TVM平账日志表
     *
     * @param swPmTvmAccountLog TVM平账日志表
     * @return R
     */
    @ApiOperation(value = "新增TVM平账日志表", notes = "新增TVM平账日志表")
    @SysLog("新增TVM平账日志表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_account_log_add')")
    public R save(@RequestBody SwPmTvmAccountLog swPmTvmAccountLog) {
        SwPmTvmAccountLog defaultObj = ObjectUtils.getDefaultObj(SwPmTvmAccountLog.class);
        BeanUtils.copyProperties(swPmTvmAccountLog, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTvmAccountLog));
        return R.ok(swPmTvmAccountLogService.save(defaultObj));
    }

    /**
     * 修改TVM平账日志表
     *
     * @param swPmTvmAccountLog TVM平账日志表
     * @return R
     */
    @ApiOperation(value = "修改TVM平账日志表", notes = "修改TVM平账日志表")
    @SysLog("修改TVM平账日志表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_account_log_edit')")
    public R updateById(@RequestBody SwPmTvmAccountLog swPmTvmAccountLog) {
        SwPmTvmAccountLog updateObj = new SwPmTvmAccountLog();
        BeanUtils.copyProperties(swPmTvmAccountLog, updateObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTvmAccountLog));
        return R.ok(swPmTvmAccountLogService.updateById(updateObj));
    }

    /**
     * 通过id删除TVM平账日志表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除TVM平账日志表", notes = "通过id删除TVM平账日志表")
    @SysLog("通过id删除TVM平账日志表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_account_log_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmTvmAccountLogService.removeById(id));
    }

    /**
     * TVM平账日志归档
     *
     * @param idList
     * @return
     */
    @ApiOperation(value = "TVM平账日志归档", notes = "TVM平账日志归档")
    @SysLog("TVM平账日志归档")
    @PostMapping("/record")
    public R record(@RequestBody List<Integer> idList) {
        List<SwPmTvmAccountLog> recordList = new ArrayList<>();
        idList.forEach(id -> {
            SwPmTvmAccountLog recordItem = new SwPmTvmAccountLog();
            recordItem.setId(id);
            recordItem.setLogStatus(LogEnum.RECORD.getCode());
            recordItem.setLogDate(LocalDateTime.now());
            recordList.add(recordItem);
        });
        return R.ok(swPmTvmAccountLogService.updateBatchById(recordList));
    }

    public QueryWrapper getQuery(SwPmTvmAccountLog swPmTvmAccountLog,QueryWrapper queryWrapper){
        if(swPmTvmAccountLog.getLineCode()!=null){
            queryWrapper.eq("line_code",swPmTvmAccountLog.getLineCode());
        }
        if(swPmTvmAccountLog.getStationCode()!=null){
            queryWrapper.eq("station_code",swPmTvmAccountLog.getStationCode());
        }
        if(swPmTvmAccountLog.getTvmCode()!=null){
            queryWrapper.eq("tvm_code",swPmTvmAccountLog.getTvmCode());
        }
        if(swPmTvmAccountLog.getAccountAmount()!=null){
            queryWrapper.eq("account_amount",swPmTvmAccountLog.getAccountAmount());
        }
        if (swPmTvmAccountLog.getAccountedDate()!=null) {
            queryWrapper.between("accounted_date", DateUtilSupport.asDateString(swPmTvmAccountLog.getAccountedDate()), DateUtilSupport.asDateString2(swPmTvmAccountLog.getAccountedDate()));
        }
        if (swPmTvmAccountLog.getAccountDate()!=null) {
            queryWrapper.between("account_date", DateUtilSupport.asDateString(swPmTvmAccountLog.getAccountDate()), DateUtilSupport.asDateString2(swPmTvmAccountLog.getAccountDate()));
        }
        return queryWrapper;
    }
}
